//
//  ViewController.m
//  二维码开发
//
//  Created by song on 15/9/18.
//  Copyright (c) 2015年 song. All rights reserved.
//

#import "ViewController.h"
#import "QREncoder.h"
#import "ZBarSDK.h"

@interface ViewController () <ZBarReaderDelegate,ZBarReaderViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImage *image = [QREncoder encode:@"http://www.baidu.com"];
    
    self.imageView.image =image;
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (IBAction)scan:(id)sender {
    
    
    
    ZBarImageScanner *scanner = [[ZBarImageScanner alloc] init];
    [scanner setSymbology:ZBAR_PARTIAL config:0 to:0];
    ZBarReaderView *readerview = [[ZBarReaderView alloc] initWithImageScanner:scanner];
    readerview.frame =CGRectMake(100, 100, 300,300);
    readerview.backgroundColor =[UIColor grayColor];

    readerview.readerDelegate = self;

    [readerview start];
    [self.view addSubview:readerview];
    
    
//    这样是不自定义视图的大小
//    ZBarReaderViewController *reader = [[ZBarReaderViewController alloc] init];
//    reader.readerDelegate = self;
//    ZBarImageScanner *scanner = reader.scanner;
//    [scanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0];
//    reader.showsZBarControls =YES;
//    [self  presentViewController:reader animated:YES completion:nil];
    
    
}

-(void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image
{
    ZBarSymbol *symbol =nil;
    for( symbol  in symbols)
    {
        break;
    }
    self.orginImageView.image = image;
    NSString *text = symbol.data;
    self.codeLable.text = text;
    [readerView stop];
}


-(void)imagePickerController:(UIImagePickerController *)reader didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    id<NSFastEnumeration> results =[info objectForKey:ZBarReaderControllerResults];
    ZBarSymbol *symbol =nil;
    for( symbol  in results)
    {
        break;
    }
    self.orginImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [reader dismissViewControllerAnimated:YES completion:nil];
    NSString *text = symbol.data;
    self.codeLable.text = text;
    
}
@end
